<?php
class SingelArticleAction extends BaseAction {
	
	//[操作流]默认页面{修改} Or {新增}
	//当传过来的ID有值，则进行修改操作流
	//当传过来的ID没值，则进行新增操作流
	public function todo(){
		$referUrl = $_SERVER['HTTP_REFERER'];
		$cid = $this->_get("cid");
		if($cid){
			$obj = SingelArticleHelper::getObject("catalog_id={$cid}");
			$cata = CatalogHelper::getObject("catalog_id={$cid}");
			$this->assign("obj",$obj);
			$this->assign("cata",$cata);
		}
		$this->assign("nowTime",time());
		$this->assign("referUrl",$referUrl);
		$this->display();
	}
	
	public function process(){
		$cid = $this->_post("cid");
		$data["content"] = htmlspecialchars(stripslashes($_POST['content']));
		$data['timeline'] = strtotime($this->_post("timeline"));
		$data['catalog_id'] = $cid;
		
		$catalog = CatalogHelper::getObject("catalog_id={$cid}");
		if(empty($catalog)){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请刷新页面后再操作！");
			return;
		}
		if(empty($data["content"])){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "内容不能为空！");
			return;
		}
		
		$obj = SingelArticleHelper::getObject("catalog_id={$cid}");
		$result = false;
		if(empty($obj)){	//执行ADD操作
			$result = SingelArticleHelper::add($data);
		}else{	//执行UPDATE操作
			$result = SingelArticleHelper::update($data,"catalog_id={$cid}");
		}
		
		if($result === false){	//失败，则提示错误信息，并返回上一个页面[记录了上一个页面，用户填写的信息]
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
		}else{	//修改成功，提示成功信息，并且返回上一个页面
			echo CommonUtil::ajaxReturn(Status::$SUCCESS, "修改成功！");
		}
	}
	
	public function delete(){
		$id = $this->_get("id");
		$obj = ArticleHelper::getObject("article_id={$id}");
		
		if(empty($obj)){
			echo CommonUtil::ajaxReturn ( Status::$ERROR, "对象不存在，操作失败！" );
			return ;
		}
		$result = ArticleHelper::delete("article_id={$id}");
		if($result === false){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return ;
		}
		echo CommonUtil::ajaxReturn(Status::$RELOAD_PAGE, "操作成功！");
	}
	
	
	
}